﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections;
using System.Globalization;
using System.Reflection;

namespace CKS.SharePoint.Workbook {
    public partial class ManageActivityLoggingControl : System.Web.UI.UserControl {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                LoadData();
            }
        }

        private void LoadData() {
            SPWeb site = SPContext.Current.Web;
            Guid listId = new Guid(Request["List"]);
            SPList list = site.Lists[listId];

            enableLoggingCheckBox.Checked = IsLoggingSet(list);
        }




        protected void cancelButton_Click(object sender, EventArgs e) {
            string sourceUrl = Request["Source"];

            if (string.IsNullOrEmpty(sourceUrl)) {
                sourceUrl = SPContext.Current.Web.Url;

                if (!sourceUrl.EndsWith("/")) {
                    sourceUrl += "/";
                }

                sourceUrl += "default.aspx";
            }

            Response.Redirect(sourceUrl);
        }

        protected void okButton_Click(object sender, EventArgs e)
        {
            SPWeb site = SPContext.Current.Web;
            Guid listId = new Guid(Request["List"]);
            SPList list = site.Lists[listId];

            if (enableLoggingCheckBox.Checked)
            {
                AddEventReceivers(list);
            }
            else
            {
                RemoveEventReceivers(list);
            }
        }

        private void AddEventReceivers(SPList list)
        {
            SPEventReceiverDefinitionCollection eventHandlers = list.EventReceivers;
            string assemblyName = Assembly.GetExecutingAssembly().FullName;
            
            SPEventReceiverDefinition erd = eventHandlers.Add();
            erd.Type = SPEventReceiverType.ItemAdded;
            erd.SequenceNumber = 1000;
            erd.Assembly = assemblyName ;
            erd.Class = ActivityLogConstants.ActivityLogEventReceiverClass;
            erd.Update();

            erd = eventHandlers.Add();
            erd.Type = SPEventReceiverType.ItemUpdated;
            erd.SequenceNumber = 1000;
            erd.Assembly = assemblyName;
            erd.Class = ActivityLogConstants.ActivityLogEventReceiverClass;
            erd.Update();

            erd = eventHandlers.Add();
            erd.Type = SPEventReceiverType.ItemCheckedIn;
            erd.SequenceNumber = 1000;
            erd.Assembly = assemblyName;
            erd.Class = ActivityLogConstants.ActivityLogEventReceiverClass;
            erd.Update();
            
            
        }

        private void RemoveEventReceivers(SPList list)
        {
            List<Guid> listToBeRemoved = new List<Guid>() ;
            SPEventReceiverDefinitionCollection eventHandlers = list.EventReceivers;

            foreach (SPEventReceiverDefinition eventReceiver in eventHandlers)
            {
                if (eventReceiver.Assembly == Assembly.GetExecutingAssembly().FullName
                    && eventReceiver.Class == ActivityLogConstants.ActivityLogEventReceiverClass)
                {
                    listToBeRemoved.Add(eventReceiver.Id);
                }
            }
            foreach (Guid id in listToBeRemoved)
            {
                eventHandlers[id].Delete();
            }
        }

        private bool IsLoggingSet(SPList list)
        {
            // Checks that the correct EventReceiver is set
            if (list.EventReceivers.Count > 0)
            {
                bool found = false;
                SPEventReceiverDefinitionCollection eventHandlers = list.EventReceivers;

                foreach (SPEventReceiverDefinition eventReceiver in eventHandlers)
                {
                    if (eventReceiver.Assembly == Assembly.GetExecutingAssembly().FullName 
                        && eventReceiver.Class == ActivityLogConstants.ActivityLogEventReceiverClass)
                    {
                        found = true;
                        break;
                    }
                }
                return found;
            }
            else
            {
                return false;
            }
        }

    }
}